package com.example.project.service;

import java.time.LocalDateTime;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.project.annotation.IssueOperation;
import com.example.project.entity.IssueOperationRecord;
import com.example.project.mapper.IssueOperationRecordMapper;
import com.example.project.service.impl.BaseServiceImpl;
import com.example.project.utils.StringUtils;

/**
 * 问题操作记录Service接口
 * 
 * @author system
 * @date 2025-11-21
 */
@Service
public class IssueOperationRecordService extends BaseServiceImpl {

	@Autowired
	private IssueOperationRecordMapper issueOperationRecordMapper;

	/**
	 * 查询问题操作记录
	 * 
	 * @param id 问题操作记录主键
	 * @return 问题操作记录
	 */
	public IssueOperationRecord selectIssueOperationRecordById(Long id) {
		return issueOperationRecordMapper.selectIssueOperationRecordById(id);
	}

	/**
	 * 查询问题操作记录列表
	 * 
	 * @param issueOperationRecord 问题操作记录
	 * @return 问题操作记录
	 */
	public List<IssueOperationRecord> selectIssueOperationRecordList(IssueOperationRecord issueOperationRecord) {
		return issueOperationRecordMapper.selectIssueOperationRecordList(issueOperationRecord);
	}

	/**
	 * 根据问题ID查询操作记录列表
	 * 
	 * @param issueId 问题ID
	 * @return 操作记录集合
	 */
	public List<IssueOperationRecord> selectRecordsByIssueId(Long issueId) {
		return issueOperationRecordMapper.selectRecordsByIssueId(issueId);
	}

	/**
	 * 根据项目ID查询操作记录列表
	 * 
	 * @param projectId 项目ID
	 * @return 操作记录集合
	 */
	public List<IssueOperationRecord> selectRecordsByProjectId(Long projectId) {
		return issueOperationRecordMapper.selectRecordsByProjectId(projectId);
	}

	/**
	 * 新增问题操作记录
	 * 
	 * @param issueOperationRecord 问题操作记录
	 * @return 结果
	 */
	public int insertIssueOperationRecord(IssueOperationRecord issueOperationRecord) {
		issueOperationRecord.setCreateTime(LocalDateTime.now());
		return issueOperationRecordMapper.insertIssueOperationRecord(issueOperationRecord);
	}

	/**
	 * 修改问题操作记录
	 * 
	 * @param issueOperationRecord 问题操作记录
	 * @return 结果
	 */
	public int updateIssueOperationRecord(IssueOperationRecord issueOperationRecord) {
		issueOperationRecord.setUpdateTime(LocalDateTime.now());
		return issueOperationRecordMapper.updateIssueOperationRecord(issueOperationRecord);
	}

	/**
	 * 批量删除问题操作记录
	 * 
	 * @param ids 需要删除的问题操作记录主键
	 * @return 结果
	 */
	public int deleteIssueOperationRecordByIds(Long[] ids) {
		return issueOperationRecordMapper.deleteIssueOperationRecordByIds(ids);
	}

	/**
	 * 删除问题操作记录信息
	 * 
	 * @param id 问题操作记录主键
	 * @return 结果
	 */
	public int deleteIssueOperationRecordById(Long id) {
		return issueOperationRecordMapper.deleteIssueOperationRecordById(id);
	}

	/**
	 * 记录操作
	 * 
	 * @param issueId       问题ID
	 * @param projectId     项目ID
	 * @param operatorId    操作人ID
	 * @param operatorName  操作人姓名
	 * @param operationType 操作类型
	 * @param customerName  客户名称
	 * @param issueSubject  需求标题
	 * @param remarks       备注
	 * @return 结果
	 */
	public int recordOperation(Long issueId, Long projectId, Long operatorId, String operatorName,
			Integer operationType, String customerName, String issueSubject, String remarks) {
		if (issueId == null || operatorId == null || operationType == null) {
			return 0;
		}

		IssueOperationRecord record = new IssueOperationRecord();
		record.setIssueId(issueId);
		record.setProjectId(projectId);
		record.setOperatorId(operatorId);
		record.setOperatorName(StringUtils.isNotEmpty(operatorName) ? operatorName : "");
		record.setOperationType(operationType);
		record.setOperationTime(LocalDateTime.now());
		record.setCustomerName(StringUtils.isNotEmpty(customerName) ? customerName : "");
		record.setRequirementName(StringUtils.isNotEmpty(issueSubject) ? issueSubject : "");
		record.setRemarks(StringUtils.isNotEmpty(remarks) ? remarks : "");

		return insertIssueOperationRecord(record);
	}

	@IssueOperation(type = 0, value = "发布需求")
	public void publicRequirement(Long issueId) {
		// 空实现，操作由AOP切面记录
	}

	/**
	 * 领取需求
	 */
	@IssueOperation(type = 1, value = "领取了需求")
	public void claimRequirement(Long issueId) {
		// 空实现，操作由AOP切面记录
	}

	/**
	 * 完成需求
	 */
	@IssueOperation(type = 2, value = "完成了需求")
	public void completeRequirement(Long issueId) {
		// 空实现，操作由AOP切面记录
	}

	/**
	 * 审核通过需求
	 */
	@IssueOperation(type = 5, value = "审核通过需求")
	public void approveRequirement(Long issueId) {
		// 空实现，操作由AOP切面记录
	}

	/**
	 * 审核不通过需求
	 */
	@IssueOperation(type = 6, value = "审核不通过需求")
	public void rejectRequirement(Long issueId) {
		// 空实现，操作由AOP切面记录
	}

	@IssueOperation(type = 7, value = "关联需求")
	public void correlationRequirement(Long issueId) {
		// 空实现，操作由AOP切面记录
	}

	@IssueOperation(type = 8, value = "取消关联需求")
	public void unlinkRequirement(Long issueId) {
		// 空实现，操作由AOP切面记录
	}
}